package com.tybbank.Utils;

import android.util.Log;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by mao on 2016/9/19.
 * 各种验证方法
 */
public class YanZhengUtil {

    private static String TAG = "TAG--VerifyUtil";
    //一个Pattern对象是一个正则表达式经编译后的表现模式
    private static Pattern mPattern;
    //一个Matcher对象是一个状态机器，它依据Pattern对象做为匹配模式对字符串展开匹配检查。
    private static Matcher mMatcher;

    //判断是不是手机号，11位
    public static boolean isPhoneNumber(String phoneNum) {
        mPattern = Pattern
                .compile("[1][3456789]\\d{9}");
        mMatcher = mPattern.matcher(phoneNum);
        return mMatcher.matches();
    }

    //验证邮箱 必须带@，并且@后必须带域名，如.com
    public static boolean isEmail(String email) {
        String str = "^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
        mPattern = Pattern.compile(str);
        mMatcher = mPattern.matcher(email);
        return mMatcher.matches();
    }

    //验证输入身份证号
    public static boolean isIDcard(String idcard) {
        Log.e(TAG, "身份证号码的长度=" + idcard.length());
        if (idcard.length() == 15) {
            //15位检验
            String str = "^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$";
            mPattern = Pattern.compile(str);
            mMatcher = mPattern.matcher(idcard);
            return mMatcher.matches();
        }
        if (idcard.length() == 18) {
            //18位检验
            String str = "^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{4}$";
            mPattern = Pattern.compile(str);
            mMatcher = mPattern.matcher(idcard);
            return mMatcher.matches();
        }
        return false;
    }

    // 验证密码 5-15位数字或英文字符（不含全角字符），区分大小写，不能带空格，不能带中文字符
    public static boolean isPassword(String password) {
        mPattern = Pattern.compile("^\\s*[^\\s\u4e00-\u9fa5]{5,15}\\s*$");
        mMatcher = mPattern.matcher(password);
        return mMatcher.matches();
    }

    //验证昵称 2-20位中英文、数字、下划线(开头结尾不能是下划线)
    public static boolean isUserName(String name) {
        mPattern = Pattern
                .compile("^(?!_)(?!.*?_$)[a-zA-Z0-9_\u4e00-\u9fa5]{2,20}+$");
        mMatcher = mPattern.matcher(name);
        return mMatcher.matches();
    }

    /**
     * 校验车牌号
     *
     * @param carNumber
     * @return
     */
    public static boolean isCarNumber(String carNumber) {
        mPattern = Pattern
                .compile("[\u4e00-\u9fa5]{1}[a-z_A-Z]{1}[a-z_A-Z_0-9]{5}");
        mMatcher = mPattern.matcher(carNumber);
        return mMatcher.matches();
    }

    /**
     * 校验银行卡卡号
     *
     * @param cardId
     * @return
     */
    public static boolean checkBankCard(String cardId) {
        char bit = getBankCardCheckCode(cardId.substring(0, cardId.length() - 1));
        return cardId.charAt(cardId.length() - 1) == bit;
    }

    /**
     * 从不含校验位的银行卡卡号采用 Luhm 校验算法获得校验位
     *
     * @param nonCheckCodeCardId
     * @return
     */
    public static char getBankCardCheckCode(String nonCheckCodeCardId) {
        if (nonCheckCodeCardId == null || nonCheckCodeCardId.trim().length() == 0
                || !nonCheckCodeCardId.matches("\\d+")) {
            throw new IllegalArgumentException("Bank card code must be number!");
        }
        char[] chs = nonCheckCodeCardId.trim().toCharArray();
        int luhmSum = 0;
        for (int i = chs.length - 1, j = 0; i >= 0; i--, j++) {
            int k = chs[i] - '0';
            if (j % 2 == 0) {
                k *= 2;
                k = k / 10 + k % 10;
            }
            luhmSum += k;
        }
        return (luhmSum % 10 == 0) ? '0' : (char) ((10 - luhmSum % 10) + '0');
    }

    //验证支付宝 必须为邮箱或者手机号
    public static boolean isAliPayAccount(String email) {
        if (YanZhengUtil.isEmail(email) || YanZhengUtil.isPhoneNumber(email)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 必须包含数字、中文、字母
     * www.yoodb.com
     * @param str
     * @return
     */
    public static boolean isLetterDigit(String str) {
        boolean isDigit = false;
        boolean isLetter = false;
        for (int i = 0; i < str.length(); i++) {
            if (Character.isDigit(str.charAt(i))) {
                isDigit = true;
            }
            if (Character.isLetter(str.charAt(i))) {
                isLetter = true;
            }
        }
        String regex = "^[a-zA-Z0-9]+$";
        boolean isRight = isDigit && isLetter && str.matches(regex);
        return isRight;
    }
}
